﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class ScreeningEndPoints
    {
        private const string ScreeningsUrl = "api/screenings";

        private HttpClient client;

        public ScreeningEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<ScreeningViewModel>> GetTodaysScreeningsForCinema(int cinemaId)
        {
            List<ScreeningViewModel> screenings = new List<ScreeningViewModel>();
            HttpResponseMessage response = await client.GetAsync("api/screeningsForCinema/" + cinemaId);
            if (response.IsSuccessStatusCode)
            {
                screenings = await response.Content.ReadAsAsync<List<ScreeningViewModel>>();
            }
            return screenings;
        }

        public async Task<List<TimeSpan>> GetAvailableStartTimesAsync(int roomId, DateTime startDate, int movieId)
        {
            List<TimeSpan> startTimes = new List<TimeSpan>();
            HttpResponseMessage response = await client.GetAsync("api/availableStartTimes/"+roomId +"/"+startDate.ToLongDateString()+"/"+movieId);
            if (response.IsSuccessStatusCode)
            {
                startTimes = await response.Content.ReadAsAsync<List<TimeSpan>>();
            }
            return startTimes;
        }

        public async Task<List<ScreeningViewModel>> GetAllScreeningsAsync()
        {
            List<ScreeningViewModel> screening = new List<ScreeningViewModel>();
            HttpResponseMessage response = await client.GetAsync(ScreeningsUrl);
            if (response.IsSuccessStatusCode)
            {
                screening = await response.Content.ReadAsAsync<List<ScreeningViewModel>>();
            }
            return screening;
        }

        public async Task<ScreeningViewModel> GetScreeningsAsync(int id)
        {
            ScreeningViewModel screening = null;
            HttpResponseMessage response = await client.GetAsync(ScreeningsUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                screening = await response.Content.ReadAsAsync<ScreeningViewModel>();
            }
            return screening;
        }

        public async Task CreateScreeningAsync(ScreeningViewModel screening)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(ScreeningsUrl, screening);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateScreeningAsync(ScreeningViewModel screening)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(ScreeningsUrl, screening);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteScreeningAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(ScreeningsUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
